﻿package frEngine.render
{
    import baseEngine.core.*;
    import baseEngine.system.*;
    import flash.geom.*;
    import frEngine.animateControler.*;
    import frEngine.core.*;
    import frEngine.render.layer.*;
    import frEngine.shader.*;
    import frEngine.shader.md5shader.*;

    public class DefaultRender extends Object implements IRender
    {
        protected var _matrix3d:Matrix3D;
        private var _defaultTexture3d:Texture3D;
        private var _worldView:Matrix3D;
        public static const instance:DefaultRender = new DefaultRender;

        public function DefaultRender()
        {
            this._matrix3d = Device3D.global;
            this._worldView = Device3D.worldView;
            this._defaultTexture3d = Global3D.nullBitmapDataTexture;
            return;
        }// end function

        public function drawEdge(param1:Mesh3D, param2:Number = 1, param3:Number = 1, param4:Number = 1) : void
        {
            var _loc_5:FrSurface3D = null;
            var _loc_6:EdgeSahder = null;
            var _loc_7:Boolean = false;
            var _loc_8:MaterialParams = null;
            _loc_5 = param1.getSurface(0);
            if (_loc_5 && _loc_5.visible)
            {
                _loc_6 = Md5MaterialManager.instance.getEdgeMaterial(0, param2, param3, param4, this._defaultTexture3d);
                _loc_7 = _loc_6.hasPrepared(param1, _loc_5);
                if (!_loc_7)
                {
                    return;
                }
                this.calculateObjectWorld(param1);
                _loc_8 = _loc_6.materialParams;
                var _loc_9:* = Global3D;
                var _loc_10:* = Global3D.objectsDrawn + 1;
                _loc_9.objectsDrawn = _loc_10;
            }
            return;
        }// end function

        public function drawDepth(param1:Mesh3D, param2:Number = 1, param3:Number = 1, param4:Number = 1, param5:Number = 1) : void
        {
            var _loc_6:FrSurface3D = null;
            var _loc_7:DepthShader = null;
            var _loc_8:Boolean = false;
            var _loc_9:MaterialParams = null;
            this.calculateObjectWorld(param1);
            var _loc_10:* = Global3D;
            var _loc_11:* = Global3D.objectsDrawn + 1;
            _loc_10.objectsDrawn = _loc_11;
            _loc_6 = param1.getSurface(0);
            if (_loc_6 && _loc_6.visible)
            {
                _loc_7 = Md5MaterialManager.instance.getDepthMaterial(0, param2, param3, param4, param5);
                _loc_8 = _loc_7.hasPrepared(param1, _loc_6);
                if (!_loc_8)
                {
                    return;
                }
                _loc_9 = _loc_7.materialParams;
            }
            return;
        }// end function

        private function drawOutLighting(param1:Mesh3D, param2:Vector3D, param3:Number) : void
        {
            var _loc_4:* = Md5MaterialManager.instance.getOutLightingMaterial(0, param2.x, param2.y, param2.z, param2.w, this._defaultTexture3d, param3);
            var _loc_5:* = param1.getSurface(0);
            var _loc_6:* = _loc_4.hasPrepared(param1, _loc_5);
            if (!_loc_6)
            {
                return;
            }
            var _loc_7:* = _loc_4.materialParams;
            return;
        }// end function

        protected function calculateObjectWorld(param1:Mesh3D) : void
        {
            this._matrix3d.copyFrom(param1.world);
            return;
        }// end function

        public function draw(param1:Mesh3D, param2:ShaderBase = null) : void
        {
            var _loc_3:FrSurface3D = null;
            var _loc_7:int = 0;
            var _loc_4:* = param1.getSurfacesLen();
            var _loc_5:* = param2 || param1.material;
            if (!_loc_5 || _loc_4 == 0)
            {
                return;
            }
            _loc_3 = param1.getSurface(0);
            if (!_loc_5.hasPrepared(param1, _loc_3))
            {
                return;
            }
            var _loc_8:* = Global3D;
            var _loc_9:* = Global3D.objectsDrawn + 1;
            _loc_8.objectsDrawn = _loc_9;
            this.calculateObjectWorld(param1);
            this._worldView.copyFrom(this._matrix3d);
            this._worldView.append(Device3D.view);
            var _loc_6:* = param1.material.materialParams;
            if (_loc_6.enableMask && param1.layer != Layer3DManager.warpLayer)
            {
                Global3D.scene.context.setStencilReferenceValue(0);
            }
            if (_loc_6.enableOutLight)
            {
                this.drawOutLighting(param1, _loc_6.outLightColor, _loc_6.outLightStrength);
            }
            if (_loc_3.indexBufferFr)
            {
                _loc_5.draw(param1, _loc_3, _loc_6.depthCompare, _loc_6.cullFace, _loc_6.depthWrite, _loc_6.sourceFactor, _loc_6.destFactor, _loc_3.firstIndex, _loc_3.indexBufferFr.numTriangles);
            }
            if (_loc_4 > 1)
            {
                _loc_7 = 1;
                while (_loc_7 < _loc_4)
                {
                    
                    _loc_3 = param1.getSurface(_loc_7);
                    if (_loc_5.hasPrepared(param1, _loc_3) && _loc_3.indexBufferFr)
                    {
                        _loc_5.draw(param1, _loc_3, _loc_6.depthCompare, _loc_6.cullFace, _loc_6.depthWrite, _loc_6.sourceFactor, _loc_6.destFactor, _loc_3.firstIndex, _loc_3.indexBufferFr.numTriangles);
                    }
                    _loc_7++;
                }
            }
            if (_loc_6.enableMask && param1.layer != Layer3DManager.warpLayer)
            {
                Global3D.scene.context.setStencilReferenceValue(1);
            }
            return;
        }// end function

    }
}
